<?php

	class QReportBase extends QPaginatedControl {
		///////////////////////////
		// Private Member Variables
		///////////////////////////
		// APPEARANCE
		protected $objAlternateRowStyle = null;
		protected $strBorderCollapse = QBorderCollapse::NotSet;
		protected $objOverrideRowStyleArray = null;
		protected $objRowStyle = null;

		// LAYOUT
		protected $intCellPadding = -1;
		protected $intCellSpacing = -1;
		protected $strGridLines = QGridLines::None;
		protected $blnShowHeader = true;
		protected $blnShowFooter = false;

		// Data
		protected $objData = array();
		protected $intCurrentRow = 0;

		protected $strTemplate;
		protected $strText;
		protected $blnHtmlEntities = false;

		//////////
		// Methods
		//////////
		public function PushRow() {
			$this->intCurrentRow++;
			$objData = array();
			foreach(func_get_args() as $objArg) {
				/**
				 * TODO Check that $objArg is a QReportCell
				 */
				array_push($objData,$objArg);
			}
			array_push($this->objData,$objData);
		}

		public function CellCount() {
			$intCount = 0;
			foreach($objData as $objCellRow) {
				$intCount += sizeof($objCellRow);
			}
			return $intCount;
		}

		public function ParsePostData() {}

		protected function GetControlHtml() {
			$this->DataBind();

			// Setup Style
			$strStyle = $this->GetStyleAttributes();
			if ($strStyle)
				$strStyle = sprintf('style="%s"', $strStyle);

			$intMaxCols = 0;
			$strReportBodyRows = array();
			foreach($this->objData as $objCellRow) {
				$i = 0;
				$strRowCells = array();
				foreach($objCellRow as $objCell) {
					array_push($strRowCells,sprintf('<td %s>%s</td>',
						$objCell->GetAttributes(),
						$objCell->Render()
					));
					$intMaxCols = (++$i > $intMaxCols?$i:$intMaxCols);
				}

				array_push($strReportBodyRows,sprintf('<tr>%s</tr>',
					implode("\n",$strRowCells)
				));
			}

			$strReportBody = sprintf('<tbody>%s</tbody>',
				implode("\n",$strReportBodyRows)
			);

			$strToReturn = sprintf('<table id="%s" %s%s>%s</table>',
				$this->strControlId,
				$this->GetAttributes(),
				$strStyle,
				$strReportBody);

			return $strToReturn;
		}

		/////////////////////////
		// Public Properties: GET
		/////////////////////////
		public function __get($strName) {
			switch ($strName) {
				// APPEARANCE
				case "AlternateRowStyle": return $this->objAlternateRowStyle;
				case "BorderCollapse": return $this->strBorderCollapse;
				case "RowStyle": return $this->objRowStyle;

				// LAYOUT
				case "CellPadding": return $this->intCellPadding;
				case "CellSpacing": return $this->intCellSpacing;
				case "GridLines": return $this->strGridLines;
				case "ShowHeader": return $this->blnShowHeader;
				case "ShowFooter": return $this->blnShowFooter;

				default:
					try {
						return parent::__get($strName);
					} catch (QCallerException $objExc) {
						$objExc->IncrementOffset();
						throw $objExc;
					}
			}
		}


		/////////////////////////
		// Public Properties: SET
		/////////////////////////
		public function __set($strName, $mixValue) {
			switch ($strName) {
				// APPEARANCE
				case "AlternateRowStyle":
					try {
						$this->objAlternateRowStyle = QType::Cast($mixValue, "QDataGridRowStyle");
						break;
					} catch (QInvalidCastException $objExc) {
						$objExc->IncrementOffset();
						throw $objExc;
					}
				case "BorderCollapse":
					try {
						$this->strBorderCollapse = QType::Cast($mixValue, QType::String);
						break;
					} catch (QInvalidCastException $objExc) {
						$objExc->IncrementOffset();
						throw $objExc;
					}
				case "RowStyle":
					try {
						$this->objRowStyle = QType::Cast($mixValue, "QDataGridRowStyle");
						break;
					} catch (QInvalidCastException $objExc) {
						$objExc->IncrementOffset();
						throw $objExc;
					}
					
				// LAYOUT
				case "CellPadding":
					try {
						$this->intCellPadding = QType::Cast($mixValue, QType::Integer);
						break;
					} catch (QInvalidCastException $objExc) {
						$objExc->IncrementOffset();
						throw $objExc;
					}
				case "CellSpacing":
					try {
						$this->intCellSpacing = QType::Cast($mixValue, QType::Integer);
						break;
					} catch (QInvalidCastException $objExc) {
						$objExc->IncrementOffset();
						throw $objExc;
					}
				case "GridLines":
					try {
						$this->strGridLines = QType::Cast($mixValue, QType::String);
						break;
					} catch (QInvalidCastException $objExc) {
						$objExc->IncrementOffset();
						throw $objExc;
					}
				case "ShowHeader":
					try {
						$this->blnShowHeader = QType::Cast($mixValue, QType::Boolean);
						break;
					} catch (QInvalidCastException $objExc) {
						$objExc->IncrementOffset();
						throw $objExc;
					}

				case "ShowFooter":
					try {
						$this->blnShowFooter = QType::Cast($mixValue, QType::Boolean);
						break;
					} catch (QInvalidCastException $objExc) {
						$objExc->IncrementOffset();
						throw $objExc;
					}

				default:
					try {
						parent::__set($strName, $mixValue);
						break;
					} catch (QCallerException $objExc) {
						$objExc->IncrementOffset();
						throw $objExc;
					}
			}
		}
	}
